icon paintable: Replace get_filename and get_resource_path with get_file()
authorAlexander Larsson <alexl@redhat.com>
Mon, 10 Feb 2020 11:33:17 +0000 (12:33 +0100)
committerAlexander Larsson <alexl@redhat.com>
Mon, 10 Feb 2020 11:33:17 +0000 (12:33 +0100)
This returns a GFile which can represent both the above.

demos/icon-browser/iconbrowserwin.c
docs/reference/gtk/gtk4-sections.txt
gtk/gtkicontheme.c
gtk/gtkicontheme.h
tests/testicontheme.c
testsuite/gtk/icontheme.c

index d8f684a7dd38285fde760410ee4b3df0c26a533f..c0fe8250317522b37d220d3bda260da0ad8352c7 100644 (file)
@@ -423,7 +423,6 @@ get_file (GValue   *value,
   GtkIconTheme *icon_theme;
   const char *name;
   GtkIconPaintable *info;
-  GFile *file;
 
   name = gtk_image_get_icon_name (GTK_IMAGE (data));
   icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (data)));
@@ -434,9 +433,7 @@ get_file (GValue   *value,
                                      32, 1,
                                      gtk_widget_get_direction (GTK_WIDGET (data)),
                                      0);
-  file = g_file_new_for_path (gtk_icon_paintable_get_filename (info));
-  g_value_set_object (value, file);
-  g_object_unref (file);
+  g_value_take_object (value, gtk_icon_paintable_get_file (info));
   g_object_unref (info);
 }
 
index aa7726bb5379ce7225dafb4419df1125380a10fa..3802214025474443663cd355d6af2c0eb3239fb4 100644 (file)
@@ -5009,8 +5009,7 @@ gtk_icon_theme_choose_icon_finish
 gtk_icon_theme_lookup_by_gicon
 gtk_icon_theme_list_icons
 gtk_icon_theme_get_icon_sizes
-gtk_icon_paintable_get_filename
-gtk_icon_paintable_get_resource_path
+gtk_icon_paintable_get_file
 gtk_icon_paintable_get_icon_name
 gtk_icon_paintable_is_symbolic
 <SUBSECTION Standard>
index 0232396f19ef69f8ad2d0857ba914eac193436f5..02ac30a197b70e3a1c1884c0049b162ec6515805 100644 (file)
@@ -3262,41 +3262,41 @@ gtk_icon_paintable_class_init (GtkIconPaintableClass *klass)
   gobject_class->finalize = gtk_icon_paintable_finalize;
 }
 
-/**
- * gtk_icon_paintable_get_filename:
- * @self: a #GtkIcon
- *
- * Gets the filename for the icon.
- *
- * Returns: (nullable) (type filename): the filename for the icon, or %NULL
- *     if the icon is not represented by a filename.
- */
-const gchar *
-gtk_icon_paintable_get_filename (GtkIconPaintable *icon)
+static GFile *
+new_resource_file (const char *filename)
 {
-  g_return_val_if_fail (icon != NULL, NULL);
+  char *escaped = g_uri_escape_string (filename,
+                                       G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
+  char *uri = g_strconcat ("resource://", escaped, NULL);
+  GFile *file = g_file_new_for_uri (uri);
 
-  if (!icon->is_resource)
-    return icon->filename;
-  return NULL;
+  g_free (escaped);
+  g_free (uri);
+
+  return file;
 }
 
 /**
- * gtk_icon_paintable_get_resource_path:
+ * gtk_icon_paintable_get_file:
  * @self: a #GtkIcon
  *
- * Gets the resource path for the icon.
+ * Gets the #GFile that was used to load the icon, or %NULL if the icon was
+ * not loaded from a file.
  *
- * Returns: (nullable) (type filename): the resource for the icon, or %NULL
- *     if the icon is not represented by a resource.
+ * Returns: (nullable) (transfer full): the #GFile for the icon, or %NULL.
+ *    Free with g_object_unref().
  */
-const gchar *
-gtk_icon_paintable_get_resource_path (GtkIconPaintable *icon)
+GFile *
+gtk_icon_paintable_get_file (GtkIconPaintable *icon)
 {
-  g_return_val_if_fail (icon != NULL, NULL);
+  if (icon->filename)
+    {
+      if (icon->is_resource)
+        return new_resource_file (icon->filename);
+      else
+        return g_file_new_for_path (icon->filename);
+    }
 
-  if (icon->is_resource)
-    return icon->filename;
   return NULL;
 }
 
index f2904708f100afd755ce138f5452bdc7b421c9b6..cf73c284cd18dd31d1a58f66b61eb03811c3f2a0 100644 (file)
@@ -140,9 +140,7 @@ GDK_AVAILABLE_IN_ALL
 GType                 gtk_icon_paintable_get_type         (void) G_GNUC_CONST;
 
 GDK_AVAILABLE_IN_ALL
-const gchar *         gtk_icon_paintable_get_filename      (GtkIconPaintable  *self);
-GDK_AVAILABLE_IN_ALL
-const gchar *         gtk_icon_paintable_get_resource_path (GtkIconPaintable  *self);
+GFile *               gtk_icon_paintable_get_file          (GtkIconPaintable  *self);
 GDK_AVAILABLE_IN_ALL
 const gchar *         gtk_icon_paintable_get_icon_name     (GtkIconPaintable  *self);
 GDK_AVAILABLE_IN_ALL
index d4b330d66401177bc8da60994e6e75dd6955f37c..d161dd6566d25b034144d375df029b80ef5dbbfd 100644 (file)
@@ -114,6 +114,8 @@ main (int argc, char *argv[])
     }
   else if (strcmp (argv[1], "lookup") == 0)
     {
+      GFile *file;
+
       if (argc < 4)
        {
          g_object_unref (icon_theme);
@@ -128,17 +130,12 @@ main (int argc, char *argv[])
        scale = atoi (argv[5]);
 
       icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], NULL, size, scale, direction, flags);
+      file = gtk_icon_paintable_get_file (icon);
       g_print ("icon for %s at %dx%d@%dx is %s\n", argv[3], size, size, scale,
-               icon ? gtk_icon_paintable_get_filename (icon) : "<none>");
-
-      if (icon)
-       {
-          GdkPaintable *paintable = GDK_PAINTABLE (icon);
+               file ? g_file_get_uri (file) : "<none>");
 
-          g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (paintable), gdk_paintable_get_intrinsic_height (paintable));
-
-         g_object_unref (icon);
-       }
+      g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon)), gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon)));
+      g_object_unref (icon);
     }
   else
     {
index 6d84adc5d803ee788f29d309d1a61572f4bda541..1760c3a4318afe9ba23be2560966e5bef079c043 100644 (file)
@@ -58,6 +58,8 @@ assert_icon_lookup_size (const char         *icon_name,
                          gint                pixbuf_size)
 {
   GtkIconPaintable *info;
+  GFile *file;
+  char *path = NULL;
 
   if (fallbacks)
     {
@@ -78,21 +80,30 @@ assert_icon_lookup_size (const char         *icon_name,
       return;
     }
 
+  file = gtk_icon_paintable_get_file (info);
+  if (file)
+    {
+      path = g_file_get_path (file);
+      g_object_unref (file);
+    }
+
   if (filename)
     {
-      if (!g_str_has_suffix (gtk_icon_paintable_get_filename (info), filename))
+      if (path == NULL || !g_str_has_suffix (path, filename))
         {
           g_error ("Icon for \"%s\" with flags %s at size %d should be \"...%s\" but is \"...%s\"",
                    icon_name, lookup_flags_to_string (flags), size,
-                   filename, gtk_icon_paintable_get_filename (info) + strlen (g_get_current_dir ()));
+                   filename, path);
           return;
         }
     }
   else
     {
-      g_assert (gtk_icon_paintable_get_filename (info) == NULL);
+      g_assert (path == NULL);
     }
 
+  g_free (path);
+
   g_assert_cmpint (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (info)), ==, size);
 
   g_object_unref (info);